$(function () {
    //获取头部、和订单状态按钮的的高度
    var headerHight = $(".header").height();
    var statusHeight = $(".statusList").height();
    //获取浏览器可工作区域的高度、宽度
    var availHeight = window.screen.availHeight;
    var availWidth = window.screen.availWidth;
    $(".orderList ul").css("width", 5 * availWidth);
    $(".orderList ul li").css("width", availWidth);
    $(".orderList ul li").css("height", Math.ceil(availHeight - (headerHight + statusHeight)));
    //获取token
    var token = localStorage.token;
    if (!token) {
        location.href = "../loginAndReg/login.html";
    };
    //获取订单列表
    var pageNum = 1;
    var flag = false;
    // var lflag = false;
    // var lpageNum = 1;
    function getOderList(status,element) {
        if (flag) return false;
        $.ajax({
            type: "get",
            url: domain.test + `/getorderlist?pageNum=${pageNum}&status=${status}`,
            beforeSend: function (xhr) {
                flag = true;
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                    var oderListDetail = response.data.data;
                    console.log(oderListDetail);
                    // if(oderListDetail.list.length===0) {
                    //     var lpullmaster = new Pull($('.likeProduct'), {
                    //         onPullUp: function () {
                    //             loadLikeProduct();
                    //         },
                    //         onPullDown:function() {
                    //             lpageNum = 1;
                    //             loadLikeProduct();
                    //         }
                    //     });
                    // }
                    if (pageNum === 1) {
                        element.html(template("allItemBox", { oderList: oderListDetail.list }));
                    } else {
                        element.append(template("allItemBox", { oderList: oderListDetail.list }));
                    }
                    pullmaster.pullDownSuccess();       //刷新成功
                    if (oderListDetail.list.length < 10 || oderListDetail.list.length === 0) {
                        pullmaster.pullUpDone();        //到底啦
                        $(".bb_pull-up").css({"display":"none"});
                        if(oderListDetail.list.length!==0) {
                            element.append('<div class="footer">已到底部</div>');
                        }
                    }
                    ++pageNum;
                    flag = false;
                }
                if (response.data.rtnCode === 401) {
                    var outTimeCode = response.data.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            },
            error: function () {
                pullmaster.pullDownFailed();        //刷新失败
                pullmaster.pullUpFailed();          //数据加载失败
            }
        });
    }
    // 下拉刷新上滑分页加载
    var pullmaster = new Pull($('.orderListBox'), {
        onPullUp: function () {
            getOderList("",$(".allOrders"));
        },
    });
    //左右滑动
    var startX,animateX,endX,sleft,lleft;
    var index = 1;
    var width = $(".orderListBox").width();
    swidth = parseFloat($(".scrollBox li").width());
    lwidth = parseFloat($(".line").width());
    var percent = lwidth/swidth;
    var percent = 0.2;
    $(".scrollBox").on("touchstart",function(e) {
        startX = e.changedTouches[0].clientX;
        sleft = parseFloat($(this).css("left"));
    });
    $(".scrollBox").on("touchmove",function(e) {
        animateX = e.changedTouches[0].clientX;
        if(startX-animateX<0) {
            if(parseFloat($(this).css("left"))===0) {
                return false;
            }
        }
        if(startX-animateX>0) {
            if(parseFloat($(this).css("left"))===-4*width) {
                return false;
            }
        }
        $(".scrollBox").css("left",animateX-startX+sleft);
        $(".line").css("left",-(animateX-startX+sleft)*0.2);
    });
    $(".scrollBox").on("touchend",function(e) {
        endX = e.changedTouches[0].clientX;
        if(startX-endX>0) {
            if(Math.abs(startX-endX)>100) {
                if(index>=5) {
                    return false;
                }
                $(this).animate({
                    "left":-index*width
                },300,function() {
                    index++;
                    pageNum=1;
                    if(index===2) {
                        getOderList(0,$(".noPay"));
                    }
                    if(index===3) {
                        getOderList(2,$(".noSend"));
                    }
                    if(index===4) {
                        getOderList(3,$(".noCatch"));
                    }
                    if(index===5) {
                        getOderList(4,$(".noComment"));
                    }
                });
                $(".line").animate({
                    "left":-(-index*width*0.2)
                },500,function() {
                    $(".statusBtn").css("color","#a3a3a3");
                    $($(".statusBtn")[index-1]).css("color","#fff");
                });
            }else {
                $(this).animate({
                    "left":-(index-1)*width
                },300) 
                $(".line").animate({
                    "left":-(-(index-1)*width*0.2)
                },500);
            }
        }
        if(startX-endX<0) {
            if(Math.abs(startX-endX)>100) {
                if(index<=1) {
                    return false;
                }
                $(this).animate({
                    "left":-(index-2)*width
                },300,function() {
                    index--;
                });
                $(".line").animate({
                    "left":-(-(index-2)*width*0.2)
                },500,function() {
                    $(".statusBtn").css("color","#a3a3a3");
                    $($(".statusBtn")[index-1]).css("color","#fff");
                });
            }else {
                $(this).animate({
                    "left":-(index-1)*width
                },300)
                $(".line").animate({
                    "left":-(-(index-1)*width*0.2)
                },500);
            }
        }
    });
    //点击切换
    $(".statusBtn").on("click",function() {
        var s_startLeft = parseFloat($(".scrollBox").css("left"));
        var l_startLeft = parseFloat($(".line").css("left"));
        var attrIndex = parseInt($(this).attr("index"));
        console.log(index);
        console.log(attrIndex);
        if(attrIndex===index) {
            return false;
        }
        if(attrIndex-index>0) {
            $(".scrollBox").animate({
                "left":-(-s_startLeft + (attrIndex-index)*width)
            },300,function() {
                index = attrIndex;
                console.log(index);
                pageNum=1;
                if(index===2) {
                    console.log(222);
                    getOderList(0,$(".noPay"));
                }
                if(index===3) {
                    console.log(3333)
                    getOderList(2,$(".noSend"));
                }
                if(index===4) {
                    console.log(4444)
                    getOderList(3,$(".noCatch"));
                }
                if(index===5) {
                    console.log(5555)
                    getOderList(4,$(".noComment"));
                }
            });
            $(".line").animate({
                "left":(l_startLeft + (attrIndex-index)*width/5)
            },300,function() {
                $(".statusBtn").css("color","#a3a3a3");
                $($(".statusBtn")[index-1]).css("color","#fff");
            });
        }
        if(attrIndex-index<0) {
            $(".scrollBox").animate({
                "left":(s_startLeft + (index-attrIndex)*width)
            },300,function() {
                index = attrIndex;
                console.log(index);
                pageNum=1;
                if(index===2) {
                    console.log(222);
                    getOderList(0,$(".noPay"));
                }
                if(index===3) {
                    console.log(3333)
                    getOderList(2,$(".noSend"));
                }
                if(index===4) {
                    console.log(4444)
                    getOderList(3,$(".noCatch"));
                }
                if(index===5) {
                    console.log(5555)
                    getOderList(4,$(".noComment"));
                }
            });
            $(".line").animate({
                "left":-(-l_startLeft + (index-attrIndex)*width/5)
            },300,function() {
                $(".statusBtn").css("color","#a3a3a3");
                $($(".statusBtn")[index-1]).css("color","#fff");
            })
        }
    });
    //订单详情跳转
    $(".scrollBox").on("click",".orderItem",function() {
        var status = parseInt($(this).attr("data-status"));
        //订单为未支付或者已取消时跳转
        if(status===0||status===-1) {
            var orderId = $(this).attr("data-orderId");
            location.href = "./orderDetail.html?orderId=" + orderId;
        }else {
            var orderSkuId = $(this).attr("data-orderSkuId");
            location.href = "./orderDetail.html?orderSkuId=" + orderSkuId;
        }
    });





    //猜你喜欢
    // function loadLikeProduct(){
    //     if (lflag) return false;
    //     $.ajax({
    //         url: domain.test + "/getlike",
    //         // url: domain.online + "/api/index/like",
    //         type: 'get',
    //         data: 'pageNum=' + lpageNum,
    //         dataType: 'json',
    //         beforeSend: function () {
    //             lflag = true;
    //         },
    //         success: function (response) {
    //             var likeProductDetail = response.data.data;
    //             // var likeProductDetail = response.data;
    //             console.log(likeProductDetail);
    //             if (lpageNum == 1) {
    //                 $('.allItemBox').html(template('itemBox', { productList: likeProductDetail.plist }));
    //             } else {
    //                 $('.allItemBox').append(template('itemBox', { productList: likeProductDetail.plist }));
    //             }
    //             // lpullmaster.pullDownSuccess();       //刷新成功
    //             if (likeProductDetail.plist.length < 10 || likeProductDetail.plist.length === 0) {
    //                 lpullmaster.pullUpDone();        //到底啦
    //                 $(".bb_pull-up").css({"display":"none"});
    //                 $(".likeIcon").css({"display":"block"});
    //                 $(".end").css({"display":"block"});
    //             }
    //             ++lpageNum;
    //             console.log(lpageNum);
    //             lflag = false;
    //         },
    //         error: function () {
    //             lpullmaster.pullDownFailed();        //刷新失败
    //             lpullmaster.pullUpFailed();          //数据加载失败
    //         }
    //     })
    // }
    // 下拉刷新上滑分页加载
    // var lpullmaster = new Pull($('.likeProduct'), {
    //     onPullUp: function () {
    //         loadLikeProduct();
    //     },
    //     onPullDown:function() {
    //         pageNum = 1;
    //         loadLikeProduct();
    //     }
    // });
    //查看详情
    $(".itemBox").on("click",".likeProductItem",function(){
        var pId = $(this).attr("data-id");
        location.href = "../classify/product.html?id=" + pId;
    });
})